
package ch.ethz.twimight.activities;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Calendar;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import mcom.cmutweet.LogJSON;
import mcom.cmutweet.RoutineTasks;
import mcom.cmutweet.LogJSON.Event;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import ch.ethz.twimight.R;
import ch.ethz.twimight.data.StatisticsDBHelper;
import ch.ethz.twimight.location.LocationHelper;
import ch.ethz.twimight.net.twitter.Tweets;
import ch.ethz.twimight.net.twitter.TwitterService;
import ch.ethz.twimight.util.Constants;
import ch.ethz.twimight.util.SDCardHelper;

/**
 * Intergrating all nudges. NUDGE_SENTIMENT: Nudging Message on top of edit box
 * Timer for Tweet button.
 */
public class NewTweetActivity extends Activity {
	static NewTweetActivity curPtr;

	private static final String TAG = "NewTweetActivity";

	String userId;
	int nudge;
	boolean retweet = false;
	String retweetTweetId = "";
	LogJSON logJSON;
	
	MyCountDownTimer timer = null;
	
	private boolean useLocation;
	private EditText text;
	private TextView characters;
	private Button cancelButton;
	private Button sendButton;

	private long isReplyTo;

	// the following are all to deal with location
	private ImageButton locationButton;
	private LinearLayout locationLayout;
	private Switch locationSwitch;

	// private boolean locationChecked;
	private TextWatcher textWatcher;

	// uploading photos
	private static final int PICK_FROM_CAMERA = 1;
	private static final int PICK_FROM_FILE = 2;
	private String tmpPhotoPath; // path storing photos on SDcard
	private String finalPhotoPath; // path storing photos on SDcard
	private String finalPhotoName; // file name of uploaded photo
	private Uri tmpPhotoUri; // uri storing temp photos
	private Uri photoUri; // uri storing photos
	private ImageView mImageView; // to display the photo to be uploaded

	private boolean hasMedia = false;
	private ImageButton uploadFromGallery;
	private ImageButton uploadFromCamera;
	private ImageButton deletePhoto;
	private ImageButton previewPhoto;
	private ImageButton photoButton;
	private Bitmap photo = null;
	private LinearLayout photoLayout;

	// SDcard helper
	private SDCardHelper sdCardHelper;

	// LOGS
	LocationHelper locHelper;
	long timestamp;
	ConnectivityManager cm;

	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tweet);
		curPtr = this;

		cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		locHelper = LocationHelper.getInstance(this);

		// SDCard helper
		sdCardHelper = new SDCardHelper();
		
		
		setupBasicButtons();

		
		
		characters = (TextView) findViewById(R.id.tweet_characters);
		characters.setText(Integer.toString(Constants.TWEET_LENGTH));

		text = (EditText) findViewById(R.id.tweetText);
		
		// Did we get some extras in the intent?
		Intent i = getIntent();
		if (i.hasExtra("text")) {
			retweet = true;
			text.setText(Html.fromHtml("<i>" + i.getStringExtra("text")
					+ "</i>"));
			retweetTweetId = i.getStringExtra("retweetTweetId");
			if(retweetTweetId==null){retweetTweetId="";}
		}else{
			text.setText("");
		}
		
		
		if (text.getText().length() == 0) {
			sendButton.setEnabled(false);
		}

		if (text.getText().length() > Constants.TWEET_LENGTH) {
			text.setText(text.getText().subSequence(0, Constants.TWEET_LENGTH));
			text.setSelection(text.getText().length());
			characters.setTextColor(Color.RED);
		}

		characters.setText(Integer.toString(Constants.TWEET_LENGTH
				- text.getText().length()));

		if (i.hasExtra("isReplyTo")) {
			isReplyTo = i.getLongExtra("isReplyTo", 0);
		}

		// Get settings and set up LogJSON
		userId = LoginActivity.getTwitterId(this);

		// Get Nudge choice from SharedPreferences
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		nudge = sp.getInt(RoutineTasks.SP_NUDGE, LogJSON.NUDGE_NONE);

		// This makes sure we do not enter more than 140 characters
		textWatcher = new TextWatcher() {
			public void afterTextChanged(Editable s) {

				int nrCharacters = Constants.TWEET_LENGTH
						- text.getText().length();

				if (nrCharacters < 0) {
					text.setText(text.getText().subSequence(0,
							Constants.TWEET_LENGTH));
					text.setSelection(text.getText().length());
					nrCharacters = Constants.TWEET_LENGTH
							- text.getText().length();
				}

				if (nrCharacters <= 0) {
					characters.setTextColor(Color.RED);
				} else {
					characters.setTextColor(Color.BLACK);
				}

				if (nrCharacters == Constants.TWEET_LENGTH) {
					sendButton.setEnabled(false);
				} else {
					sendButton.setEnabled(true);
				}

				characters.setText(Integer.toString(nrCharacters));

			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}
		};
		text.addTextChangedListener(textWatcher);
		text.setSelection(text.getText().length());

		setupImageRelatedButtons();

		
		logJSON = new LogJSON(LogJSON.TYPE_PUBLISH, nudge, retweet, useLocation);
		logJSON.addEvent(LogJSON.Event.EVENT_EDIT_START, 
				Calendar.getInstance().getTimeInMillis(), 
				text.getText().toString(), retweetTweetId);
	}

	void setupImageRelatedButtons() {

		// uploading photos
		tmpPhotoPath = Tweets.PHOTO_PATH + "/" + "tmp";
		finalPhotoPath = Tweets.PHOTO_PATH + "/"
				+ LoginActivity.getTwitterId(this);
		mImageView = new ImageView(this);

		photoLayout = (LinearLayout) findViewById(R.id.linearLayout_photo_view);
		photoLayout.setVisibility(View.GONE);

		uploadFromGallery = (ImageButton) findViewById(R.id.upload_from_gallery);
		uploadFromGallery.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				uploadFromGallery();
			}
		});

		uploadFromCamera = (ImageButton) findViewById(R.id.upload_from_camera);
		uploadFromCamera.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				uploadFromCamera();
			}
		});

		previewPhoto = (ImageButton) findViewById(R.id.preview_photo);
		previewPhoto.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				mImageView = new ImageView(NewTweetActivity.this);
				mImageView.setImageBitmap(photo);
				AlertDialog.Builder photoPreviewDialog = new AlertDialog.Builder(
						NewTweetActivity.this);
				photoPreviewDialog.setView(mImageView);
				photoPreviewDialog.setNegativeButton("close", null);
				photoPreviewDialog.show();

			}
		});

		deletePhoto = (ImageButton) findViewById(R.id.delete_photo);
		deletePhoto.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				sdCardHelper.deleteFile(tmpPhotoUri.getPath());
				hasMedia = false;
				setButtonStatus(true, false);

				// Log image removal
				logJSON.addEvent(LogJSON.Event.EVENT_IMAGE_OFF, Calendar
						.getInstance().getTimeInMillis(), text.getText()
						.toString(), "");
			}
		});

		String[] filePaths = { tmpPhotoPath, finalPhotoPath };
		if (sdCardHelper.checkSDState(filePaths)) {

			sdCardHelper.clearTempDirectory(tmpPhotoPath);
			setButtonStatus(true, false);
		} else
			setButtonStatus(false, false);
	}

	
	final int LL_LOCATION = R.id.linearLayout_location_view;
	final int LL_PHOTO = R.id.linearLayout_photo_view;
	int currentLayoutId = -1;
	void setLinearLayout(int layoutId){
		hideSoftKeyboard(text);
		
		if(layoutId == currentLayoutId){
			((LinearLayout)findViewById(currentLayoutId)).setVisibility(View.GONE);
			currentLayoutId = -1;
		}
		else {
			if(currentLayoutId != -1){
				((LinearLayout)findViewById(currentLayoutId)).setVisibility(View.GONE);
			}
			((LinearLayout)findViewById(layoutId)).setVisibility(View.VISIBLE);
			currentLayoutId = layoutId;
		}
	}
	
	void setupBasicButtons() {

		cancelButton = (Button) findViewById(R.id.tweet_cancel);
		cancelButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				if (nudge == LogJSON.NUDGE_TIMERICON && timer != null) {
					// Cancel the timer
					onTimerCanceled();
				} else {
					// Log EDIT_CANCEL
					logJSON.addEvent(LogJSON.Event.EVENT_EDIT_CANCEL, Calendar
							.getInstance().getTimeInMillis(), text.getText()
							.toString(), "");
					
					// Send LogJSON
					sendLog();

					finish();
				}
			}

		});

		sendButton = (Button) findViewById(R.id.tweet_send);
		sendButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				if (nudge == LogJSON.NUDGE_TIMERICON) {

					// Hide Soft Keyboard
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(
							NewTweetActivity.curPtr.text.getWindowToken(), 0);

					if(timer==null){// Start timer
						onTimerStart();
					}else{//Enforced Tweeting after Melt
						onTimerBypass();
					}
					
				} else {
					sendTweet();
				}
			}

		});

		photoButton = (ImageButton) findViewById(R.id.tweet_photo);
		photoButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setLinearLayout(LL_PHOTO);
			}
		});

		// User settings: do we use location or not?
		useLocation = PreferenceManager
				.getDefaultSharedPreferences(this)
				.getBoolean("prefUseLocation", Constants.TWEET_DEFAULT_LOCATION);
		
		locationButton = (ImageButton) findViewById(R.id.tweet_location);
		locationLayout = (LinearLayout)findViewById(R.id.linearLayout_location_view);
		locationSwitch = (Switch)findViewById(R.id.tweet_location_switch);
		
		if (useLocation) {
			locationButton.setImageResource(R.drawable.ic_menu_location_on);
			locationSwitch.setChecked(true);
		}else{
			locationButton.setImageResource(R.drawable.ic_menu_location_off);
			locationSwitch.setChecked(false);
		}
		
		locationButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setLinearLayout(LL_LOCATION);
			}
		});
		locationLayout.setVisibility(View.GONE);
		locationSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener(){
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (!useLocation) {
					locHelper.registerLocationListener();
					locationButton.setImageResource(R.drawable.ic_menu_location_on);
					useLocation = true;
					logJSON.addEvent(LogJSON.Event.EVENT_LOCATION_ON, Calendar
							.getInstance().getTimeInMillis(), text.getText()
							.toString(), "");

				} else {
					locHelper.unRegisterLocationListener();
					locationButton.setImageResource(R.drawable.ic_menu_location_off);
					useLocation = false;
					logJSON.addEvent(LogJSON.Event.EVENT_LOCATION_OFF, Calendar
							.getInstance().getTimeInMillis(), text.getText()
							.toString(), "");
				}

				// remember location choices in SharePreferences
				SharedPreferences pref = PreferenceManager
						.getDefaultSharedPreferences(getApplicationContext());
				SharedPreferences.Editor editor = pref.edit();
				editor.putBoolean("prefUseLocation", useLocation);
				editor.commit();
			}
		});
	}

	

	/**
	 * set button status with different operations
	 * 
	 * @param statusUpload
	 * @param statusDelete
	 */
	private void setButtonStatus(boolean statusUpload, boolean statusDelete) {
		uploadFromGallery.setEnabled(statusUpload);
		uploadFromCamera.setEnabled(statusUpload);
		deletePhoto.setEnabled(statusDelete);
		previewPhoto.setEnabled(statusDelete);
		if (statusUpload) {
			uploadFromGallery.setImageResource(R.drawable.ic_menu_slideshow);
			uploadFromCamera.setImageResource(R.drawable.ic_camera);
		} else {
			uploadFromGallery
					.setImageResource(R.drawable.ic_menu_slideshow_off);
			uploadFromCamera.setImageResource(R.drawable.ic_camera_off);
		}
		if (statusDelete) {
			deletePhoto.setImageResource(R.drawable.ic_menu_delete);
			previewPhoto.setImageResource(R.drawable.ic_menu_zoom);
		} else {
			deletePhoto.setImageResource(R.drawable.ic_menu_delete_off);
			previewPhoto.setImageResource(R.drawable.ic_menu_zoom_off);
		}
	}

	/**
	 * onResume
	 */
	@Override
	public void onResume() {
		super.onResume();
		if (useLocation) {
			locHelper.registerLocationListener();
		}
	}

	/**
	 * onPause
	 */
	@Override
	public void onPause() {
		super.onPause();
		locHelper.unRegisterLocationListener();
	}

	/**
	 * On Destroy
	 */
	@Override
	public void onDestroy() {

		// If timer!=null, the activity would be halted.
		if (timer != null) {
			onTimerCanceled();
			// Log EDIT_CANCEL
			logJSON.addEvent(LogJSON.Event.EVENT_EDIT_CANCEL, Calendar
					.getInstance().getTimeInMillis(),
					text.getText().toString(), "");
		}
		sendLog();

		super.onDestroy();
		if (hasMedia) {
			sdCardHelper.deleteFile(tmpPhotoUri.getPath());
			hasMedia = false;
		}
		if (locHelper != null)
			locHelper.unRegisterLocationListener();

		locationButton.setOnClickListener(null);
		locationButton = null;

		cancelButton.setOnClickListener(null);
		cancelButton = null;

		sendButton.setOnClickListener(null);
		sendButton = null;

		text.removeTextChangedListener(textWatcher);
		text = null;
		textWatcher = null;

		// unbindDrawables(findViewById(R.id.showNewTweetRoot));
	}

	/**
	 * Checks whether we are in disaster mode and inserts the content values
	 * into the content provider.
	 * 
	 * @author pcarta
	 * 
	 */
	private class SendTweetTask extends AsyncTask<Void, Void, Boolean> {

		Uri insertUri = null;
		StatisticsDBHelper statsDBHelper;

		@Override
		protected Boolean doInBackground(Void... params) {
			boolean result = false;

			// Statistics
			statsDBHelper = new StatisticsDBHelper(getApplicationContext());
			statsDBHelper.open();
			timestamp = System.currentTimeMillis();

			if (hasMedia) {
				try {
					finalPhotoName = "twimight" + String.valueOf(timestamp)
							+ ".jpg";
					photoUri = Uri.fromFile(sdCardHelper.getFileFromSDCard(
							finalPhotoPath, finalPhotoName));// photoFileParent,
																// photoFilename));
					String fromFile = tmpPhotoUri.getPath();
					String toFile = photoUri.getPath();
					if (TwimightBaseActivity.D)
						Log.i(TAG, fromFile);
					if (TwimightBaseActivity.D)
						Log.i(TAG, toFile);
					if (sdCardHelper.copyFile(fromFile, toFile)) {

						if (TwimightBaseActivity.D)
							Log.i(TAG, "file copy successful");

					}
				} catch (Exception e) {
					if (TwimightBaseActivity.D)
						Log.d("photo", "exception!!!!!");
				}
			}
			// if no connectivity, notify user that the tweet will be send later

			ContentValues cv = createContentValues();

			// our own tweets go into the timeline buffer
			cv.put(Tweets.COL_BUFFER, Tweets.BUFFER_TIMELINE);

			insertUri = getContentResolver().insert(
					Uri.parse("content://" + Tweets.TWEET_AUTHORITY + "/"
							+ Tweets.TWEETS + "/"
							+ Tweets.TWEETS_TABLE_TIMELINE + "/"
							+ Tweets.TWEETS_SOURCE_NORMAL), cv);
			
			getContentResolver().notifyChange(Tweets.CONTENT_URI, null);
			
			//getContentResolver().notifyChange(insertUri, null);
			ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
			if (cm.getActiveNetworkInfo() == null
					|| !cm.getActiveNetworkInfo().isConnected()) {
				result = true;
			}

			if (locHelper.getCount() > 0 && cm.getActiveNetworkInfo() != null) {

				//Log.i(TAG, "writing log");
				statsDBHelper.insertRow(locHelper.getLocation(), cm
						.getActiveNetworkInfo().getTypeName(),
						StatisticsDBHelper.TWEET_WRITTEN, null, timestamp);
				locHelper.unRegisterLocationListener();
				Log.i(TAG, String.valueOf(hasMedia));
			}

			return result;

		}

		@Override
		protected void onPostExecute(Boolean result) {
			if (result)
				Toast.makeText(NewTweetActivity.this,
						getString(R.string.no_connection4), Toast.LENGTH_SHORT)
						.show();

			if (insertUri != null) {
				// schedule the tweet for uploading to twitter
				Intent i = new Intent(NewTweetActivity.this,
						TwitterService.class);
				i.putExtra("synch_request", TwitterService.SYNCH_TWEET);
				i.putExtra("rowId", new Long(insertUri.getLastPathSegment()));
				startService(i);
			}
			finish();
		}
	}

	/**
	 * Prepares the content values of the tweet for insertion into the DB.
	 * 
	 * @return
	 */
	private ContentValues createContentValues() {
		ContentValues tweetContentValues = new ContentValues();

		tweetContentValues.put(Tweets.COL_TEXT, text.getText().toString());
		tweetContentValues.put(Tweets.COL_USER,
				LoginActivity.getTwitterId(this));
		tweetContentValues.put(Tweets.COL_SCREENNAME,
				LoginActivity.getTwitterScreenname(this));
		if (isReplyTo > 0) {
			tweetContentValues.put(Tweets.COL_REPLYTO, isReplyTo);
		}

		// we mark the tweet for posting to twitter
		tweetContentValues.put(Tweets.COL_FLAGS, Tweets.FLAG_TO_INSERT);
		// set the current timestamp
		tweetContentValues.put(Tweets.COL_CREATED, System.currentTimeMillis());

		if (useLocation) {
			Location loc = locHelper.getLocation();
			if (loc != null) {
				tweetContentValues.put(Tweets.COL_LAT, loc.getLatitude());
				tweetContentValues.put(Tweets.COL_LNG, loc.getLongitude());
			}
		}
		// if there is a photo, put the path of photo in the cv
		if (hasMedia) {
			tweetContentValues.put(Tweets.COL_MEDIA, finalPhotoName);
			Log.i(TAG, Tweets.COL_MEDIA + ":" + finalPhotoName);
		}

		return tweetContentValues;
	}

	// methods photo uploading

	/**
	 * upload photo from camera
	 */
	private void uploadFromCamera() {

		if ((tmpPhotoUri = sdCardHelper.createTmpPhotoStoragePath(tmpPhotoPath)) != null) {
			Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

			intent.putExtra(MediaStore.EXTRA_OUTPUT, tmpPhotoUri);

			try {
				intent.putExtra("return-data", true);
				startActivityForResult(intent, PICK_FROM_CAMERA);
			} catch (ActivityNotFoundException e) {
				e.printStackTrace();
			}
		} else {
			Log.i(TAG, "path for storing photos cannot be created!");
			setButtonStatus(false, false);
		}

	}

	/**
	 * upload photo by taking a picture
	 */
	private void uploadFromGallery() {
		if ((tmpPhotoUri = sdCardHelper.createTmpPhotoStoragePath(tmpPhotoPath)) != null) {
			Intent intent = new Intent();
			intent.setType("image/*");
			intent.setAction(Intent.ACTION_GET_CONTENT);
			startActivityForResult(
					Intent.createChooser(intent, getString(R.string.picker)),
					PICK_FROM_FILE);
		} else {
			Log.i(TAG, "path for storing photos cannot be created!");
			setButtonStatus(false, false);
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != RESULT_OK)
			return;
		setButtonStatus(false, true);

		// Log Image Adding
		logJSON.addEvent(LogJSON.Event.EVENT_IMAGE_ON, Calendar.getInstance()
				.getTimeInMillis(), text.getText().toString(), requestCode + "");

		hasMedia = true;
		switch (requestCode) {
		case PICK_FROM_CAMERA:

			// display the picture
			photo = sdCardHelper.decodeBitmapFile(tmpPhotoUri.getPath());
			mImageView.setImageBitmap(photo);

			break;

		case PICK_FROM_FILE:

			// display the photo
			Uri mImageGalleryUri = data.getData();

			// get the real path for chosen photo
			mImageGalleryUri = Uri.parse(sdCardHelper.getRealPathFromUri(
					(Activity) NewTweetActivity.this, mImageGalleryUri));

			// copy the photo from gallery to tmp directory

			String fromFile = mImageGalleryUri.getPath();
			String toFile = tmpPhotoUri.getPath();
			if (sdCardHelper.copyFile(fromFile, toFile)) {
				photo = sdCardHelper.decodeBitmapFile(toFile);
				mImageView.setImageBitmap(photo);
			}
			break;
		}
	}

	
	
	
	
	
	
	
	
	
	
	
	void sendTweet() {
		
		logJSON.addEvent(LogJSON.Event.EVENT_EDIT_TWEET, Calendar
				.getInstance().getTimeInMillis(), text.getText()
				.toString(), "");
		
		//If Notice Nudge, Change icon SharedPreference.
		if(nudge == LogJSON.NUDGE_TIMERICON){
			
			SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(getApplicationContext());
			SharedPreferences.Editor editor = pref.edit();
			editor.putBoolean(RoutineTasks.SP_NUDGETABICON, true);
			editor.commit();
			
			RoutineTasks.startLogTask(Calendar.getInstance().getTimeInMillis(), 
					userId, RoutineTasks.LOG_ICON, "set");
			
			
			//if(ShowTweetListActivity.curPtr != null){
			//	ShowTweetListActivity.curPtr.checkTabIcon();
			//}
		}
		
		sendButton.setEnabled(false);
		sendButton.setText("Sending...");
		cancelButton.setEnabled(false);
		
		// Send LogJSON
		sendLog();

		new SendTweetTask().execute();
		//finish();
	}
	
	boolean logSent = false;
	void sendLog(){
		if(!logSent){
			RoutineTasks.startLogTask(Calendar.getInstance().getTimeInMillis(),
				userId, RoutineTasks.LOG_TWEET, logJSON.toJSONStr());
			logSent = true;
		}
	}
	
	static class MyCountDownTimer extends CountDownTimer {
		static final long LENGTH = 7000;
		static final long FREEZE = 4000;
		static final long TICK = 1000;

		public MyCountDownTimer(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish() {
			NewTweetActivity.curPtr.onTimerFinish();
		}

		@Override
		public void onTick(long millisUntilFinished) {
			long secondsLeft = millisUntilFinished / 1000;
			if (NewTweetActivity.curPtr.sendButton != null) {
				NewTweetActivity.curPtr.sendButton.setText("Confirm ("
						+ secondsLeft + ")");
				if(millisUntilFinished < FREEZE){
					NewTweetActivity.curPtr.onTimerMelt();
				}
			}
		}
	}
	
	void onTimerStart(){
		
		logJSON.addEvent(LogJSON.Event.EVENT_TIMER_START, 
				Calendar.getInstance().getTimeInMillis(),
				text.getText().toString(), "");
		timer = new MyCountDownTimer(MyCountDownTimer.LENGTH,
				MyCountDownTimer.TICK);
		timer.start();
		
		text.setEnabled(false);
		sendButton.setEnabled(false);
	}
	void onTimerMelt(){
		sendButton.setEnabled(true);
	}
	void onTimerBypass(){
		logJSON.addEvent(LogJSON.Event.EVENT_TIMER_BYPASS, 
				Calendar.getInstance().getTimeInMillis(),
				text.getText().toString(), "");
		
		timer.cancel();
		timer = null;
		sendButton.setEnabled(true);
		sendButton.setText("Tweet");
		sendTweet();
	}
	void onTimerFinish() {
		logJSON.addEvent(LogJSON.Event.EVENT_TIMER_FINISH, 
				Calendar.getInstance().getTimeInMillis(),
				text.getText().toString(), "");
		sendButton.setEnabled(true);
		sendButton.setText("Tweet");
		timer = null;

		sendTweet();
	}
	void onTimerCanceled() {
		
		logJSON.addEvent(LogJSON.Event.EVENT_TIMER_CANCEL, 
				Calendar.getInstance().getTimeInMillis(),
				text.getText().toString(), "");
		
		text.setEnabled(true);
		timer.cancel();
		timer = null;
		sendButton.setEnabled(true);
		sendButton.setText("Tweet");
	}

	/** Stop App from restarting when orientation changed */
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}
	
	public void hideSoftKeyboard(EditText editText){
    	InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    	imm.hideSoftInputFromWindow(editText.getWindowToken(),0);
    }
}
